/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package yancy.modules.sys.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import yancy.common.annotation.SysLog;
import yancy.common.utils.Constant;
import yancy.common.utils.PageUtils;
import yancy.common.utils.R;
import yancy.common.validator.ValidatorUtils;
import yancy.common.validator.group.AddGroup;
import yancy.common.validator.group.UpdateGroup;
import yancy.modules.sys.entity.SysAuthor;
import yancy.modules.sys.service.SysAuthorService;
import yancy.modules.sys.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/author")
public class SysAuthorController extends AbstractController {
    @Autowired
    private SysAuthorService sysAuthorService;
    @Autowired
    private SysUserRoleService sysUserRoleService;


    /**
     * 所有用户列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //只有超级管理员，才能查看所有管理员列表
//        if (getUserId() != Constant.SUPER_ADMIN) {
//            params.put("createUserId", getUserId());
//        }
        PageUtils page = sysAuthorService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取登录的用户信息
     */
    @GetMapping("/info")
    public R info() {
        return R.ok().put("user", getUser());
    }


    /**
     * 用户信息
     */
    @GetMapping("/info/{authorId}")
    public R info(@PathVariable("authorId") Long authorId) {
        SysAuthor author = sysAuthorService.getById(authorId);

//		//获取用户所属的角色列表
//		List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
//		user.setRoleIdList(roleIdList);

        return R.ok().put("author", author);
    }


    /**
     * 保存用户
     */
    @SysLog("保存用户")
    @PostMapping("/save")
    public R save(@RequestBody SysAuthor author) {
        ValidatorUtils.validateEntity(author, AddGroup.class);

        author.setCreateUserId(getUserId());
        author.setCreateTime(new Date());
        sysAuthorService.save(author);

        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @PostMapping("/update")

    public R update(@RequestBody SysAuthor author) {
        ValidatorUtils.validateEntity(author, UpdateGroup.class);
        author.setCreateUserId(getUserId());
        sysAuthorService.updateById(author);
        return R.ok();
    }

    /**
     * 角色列表
     */
    @GetMapping("/select")
    public R select() {
        Map<String, Object> map = new HashMap<>();

        //如果不是超级管理员，则只查询自己所拥有的角色列表
//        if (getUserId() != Constant.SUPER_ADMIN) {
//            map.put("create_user_id", getUserId());
//        }
        map.put("status", 1);
        List<SysAuthor> list = (List<SysAuthor>) sysAuthorService.listByMap(map);

        return R.ok().put("list", list);
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] authorIds) {
        if (authorIds == null) return R.ok();
        StringBuilder retIds = new StringBuilder();
        for (Long authorId : authorIds) {
            if (!sysAuthorService.deleteById(authorId)) {
                retIds.append(authorId).append(",");
            }
        }
        if (retIds.length() != 0) {
            retIds.deleteCharAt(retIds.length() - 1);
        }

        return retIds.length() != 0 ? R.error(retIds.toString() + "不能被删除") : R.ok();
    }

    @PostMapping("/delet2e")
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean yummyMethod(SysAuthor author) {
        String name = author.getUsername();
        boolean success = sysAuthorService.save(author);
        if (StringUtils.containsAny(name, "248")) {
            throw new UnsupportedOperationException();
        }
//        throw new UnsupportedOperationException();
        return success;

    }
}
